package com.example.myspringtest.handler;

import com.example.myspringtest.entity.ActConfig;
import com.example.myspringtest.service.ActLiveHandler;
import com.example.myspringtest.service.ActRankHandler;
import com.example.myspringtest.service.ActSendGiftHandler;
import com.example.myspringtest.service.ActTaskHandler;
import com.example.myspringtest.service.base.BaseFullHandler;
import com.example.myspringtest.utils.CglibFactory;
import com.example.myspringtest.utils.ContextUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 生成活动代理类 并注册到 容器中
 */
@Service
public class ReloadActHandler {

    private static final Log logger = LogFactory.getLog(ReloadActHandler.class);

    public void load(boolean sendEvent) {

        AnnotationConfigServletWebServerApplicationContext applicationContext = ContextUtils.getApplicationContext();
        List<ActConfig> configs = listConfigs();
        boolean flag = false;

        for (ActConfig config : configs) {
            if (!applicationContext.containsBean(config.getActName())) {

                // 每个业务类初始化 一个实例对象
                Object proxyBizHandler = new CglibFactory(new BaseFullHandler()).myCglibCreator(BaseFullHandler.class, config.getInterfaces());
                Class<?>[] interfaces = proxyBizHandler.getClass().getInterfaces();
                logger.info(config);
                for (Class<?> aClass : interfaces) {
                    logger.info(aClass.getSimpleName());
                }
                logger.info("============================================================================================");

                // 将生成的代理类注册到 容器中
                BaseFullHandler proxyHandler = (BaseFullHandler) proxyBizHandler;
                RootBeanDefinition proxy = new RootBeanDefinition(proxyHandler.getClass());
                applicationContext.registerBeanDefinition(config.getActName(), proxy);
                BaseFullHandler bean = (BaseFullHandler) ContextUtils.getApplicationContext().getBean(config.getActName());
                bean.setId(config.getActId());
                bean.setActName(config.getActName());
                flag = true;

            }
        }

        // 发送上下文刷新事件
        if (flag && sendEvent) {
            applicationContext.publishEvent(new ContextRefreshedEvent(applicationContext));
        }
    }

    /**
     * 读取配置
     * todo 通过数据库获取数据
     *
     * @return 活动配置
     */
    private List<ActConfig> listConfigs() {

        List<ActConfig> configs = new ArrayList<>();

        // 01 活动1配置
        ActConfig config1 = new ActConfig();
        config1.setActId(1);
        config1.setActName("actHandler1");
        config1.setInterfaces(new Class[]{ActLiveHandler.class, ActRankHandler.class});
        configs.add(config1);

        // 01 活动2配置
        ActConfig config2 = new ActConfig();
        config2.setActId(2);
        config2.setActName("actHandler2");
        config2.setInterfaces(new Class[]{ActLiveHandler.class, ActRankHandler.class, ActSendGiftHandler.class, ActTaskHandler.class});
        configs.add(config2);

        // 01 活动3配置
        ActConfig config3 = new ActConfig();
        config3.setActId(3);
        config3.setActName("actHandler3");
        config3.setInterfaces(new Class[]{ActSendGiftHandler.class, ActTaskHandler.class});
        configs.add(config3);

        return configs;

    }


}
